<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss'><id>tag:blogger.com,1999:blog-7558958</id><updated>2009-11-06T10:26:41.504+03:00</updated><title type='text'>Volodya's blog</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://vladimir_prus.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7558958/posts/default'/><link rel='alternate' type='text/html' href='http://vladimir_prus.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/7558958/posts/default?start-index=26&amp;max-results=25'/><author><name>Vladimir Prus</name><uri>http://www.blogger.com/profile/15916897773452934224</uri><email>noreply@blogger.com</email></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>36</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-7558958.post-4159580028601221130</id><published>2009-06-06T21:24:00.006+04:00</published><updated>2009-09-19T18:49:46.688+04:00</updated><title type='text'>STL Visualization</title><content type='html'>As of today, KDevelop can nicely display std::vector&lt;std::string&gt;. I'll probably omit the obvious snapshot, and will point to a &lt;a href="http://lists.kde.org/?l=kdevelop&amp;m=125326438617051&amp;w=2"&gt;mailing list post&lt;/a&gt; with instructions for trying it. Instead, I'll tell the story of this feature.

&lt;p&gt;
For its entire history, GDB did not have any official way to display types from the C++ Standard Library in a sensible way. Several third-party scripts appeared, written in GDB's internal scripting language. However, they were fairly limited. You had to explicitly run those scripts, and all you got was text output without structure, making robust IDE integration impossible. Also, GDB's scripting language is itself unpleasant, and does not even have access to internal data structures and functions. It was clear that we need a way to write pretty-printers using real scripting language, with full access to GDB data structures, and proper integration with frontend interface.

&lt;p&gt;
The first prototype of Python-based pretty printing was written by myself during free hack slot at a CodeSourcery company meeting. It took maybe 4 hours, if not less, and could display std::string as string automatically. Some 4 hours more lead to the &lt;a href="http://sourceware.org/ml/gdb/2008-02/msg00140.html"&gt;first public prototype&lt;/a&gt;. This version could automatically display std::vector as "[1,2]". The &lt;a href="http://sourceware.org/ml/gdb/2008-04/msg00189.html"&gt;second prototype&lt;/a&gt; could finally display elements of std::vector as children, like one would expect in a variables tree of a frontend, and even report when new elements are added to the vector. However, this version took a couple of days of work, exposed a mere 4 functions from GDB to Python, and was a mess internally. It was clearly already outside the "quick hack" range.

&lt;p&gt;
Those prototypes would never turn into anything, were it not for &lt;a href="http://tromey.com/blog/"&gt;Tom Tromey&lt;/a&gt; and &lt;a href="http://blog.bauermann.eng.br/category/english/"&gt;Thiago Bauermann&lt;/a&gt;, who started a project to add complete Python scripting to GDB. This is much more ambitious than just pretty-printing. In particular, it includes defining new commands in Python, with full access to GDB internals. You can read more details in a &lt;a href="http://tromey.com/blog/?p=494"&gt;post series&lt;/a&gt; by Tom. 

&lt;p&gt;
Pretty-printing became a part of that large effort, and was greatly improved. One of the most notable change was incremental fetch of children. According to the C++ standard, an object does not exist until its constructor has exited. However, gcc debug info just lists all local variables in a block. A naive pretty-printer, when invoked on such a variable, would likely go into uncharted part of memory trying to fetch all children, and never return. To fix this, the Python pretty-printers were designed to use incremental fetch, using Python iterators, and GDB MI interface was also adjusted to be more incremental (yes, it's a &lt;a href="http://vladimir_prus.blogspot.com/2007/02/debugger-stories-stack-widget.html"&gt;trend&lt;/a&gt;). Beyond that, we've spend at least 3 weeks iterating on finer details. The GDB patch was finally checked in on Sep 15, and KDevelop4 patch shortly after.

&lt;p&gt;
This is still early implementation, and might have bugs, but now it's out for everybody to try.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7558958-4159580028601221130?l=vladimir_prus.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vladimir_prus.blogspot.com/feeds/4159580028601221130/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=7558958&amp;postID=4159580028601221130' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7558958/posts/default/4159580028601221130'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7558958/posts/default/4159580028601221130'/><link rel='alternate' type='text/html' href='http://vladimir_prus.blogspot.com/2009/06/stl-visualization.html' title='STL Visualization'/><author><name>Vladimir Prus</name><uri>http://www.blogger.com/profile/15916897773452934224</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='16858447227880110707'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7558958.post-8368718919825441328</id><published>2009-06-01T14:41:00.004+04:00</published><updated>2009-06-01T16:19:19.451+04:00</updated><title type='text'>Linking 101</title><content type='html'>&lt;p&gt;Recently, I see more and more people having trouble with link-time errors—as if such an error is the worst kind of luck and cannot be fixed by  mere mortals. There are many possible reasons, including Java as default language in universities, and alarming spread of header-only-philia, but that's for another post. Here, I want to give a simple diagnostic procedure for link-time errors.
&lt;/p&gt;&lt;p&gt;
Let's lay some groundwork first. If your job is programming in C++, you need to know what the &lt;code&gt;-I&lt;/code&gt; and &lt;code&gt;-L&lt;/code&gt; options do, and how they are different. Also, given a full path to a library file (with &lt;code&gt;.a&lt;/code&gt; or &lt;code&gt;.so&lt;/code&gt; or &lt;code&gt;.lib&lt;/code&gt; extension), you should be able to link to that file—in two different ways. If you don't know any of the above already, all hope is lost—you might want to consider other occupations. Otherwise, let's look at the diagnosis steps for most common error—&lt;code&gt;'undefined symbol'&lt;/code&gt;.
&lt;/p&gt;&lt;p&gt;
First, understand where the missing symbol is supposedly defined. Educated guess is usually fine. For example, a symbol named &lt;code&gt;boost::system::foobar&lt;/code&gt; is most likely contained in the Boost.System library (and it's surprising how many folks fail to guess so). Then, find how you are supposed to link to that &lt;em&gt;logical&lt;/em&gt; component, using documentation for the component or the corresponding Linux package. For example, you might decide to add &lt;code&gt;-lboost_filesystem&lt;/code&gt; to the linker command line.

&lt;/p&gt;&lt;p&gt;
Second, make sure that used &lt;em&gt;physical&lt;/em&gt; library file is the right one, and that the linker is not picking a different version of the library from a directory you don't expect. If you get error during linking of your application, you can use the &lt;code&gt;-t&lt;/code&gt; flag for the GNU linker (or use &lt;code&gt;-Wl,-t&lt;/code&gt; on gcc command line). This will print full paths for every library used, including those specified with the &lt;code&gt;-lfoo&lt;/code&gt; syntax. For static linking, this will also tell which object files from the static libraries were used.  If you get error when running the application, you one can use the &lt;code&gt;LD_DEBUG&lt;/code&gt; environment variable. If you set that variable to &lt;code&gt;help&lt;/code&gt; prior to running your program, you'll get a list of possible values. The most handy value in our case is &lt;code&gt;files&lt;/code&gt;.

&lt;/p&gt;&lt;p&gt;
Third, if you seem to link to the right library, there are three further  possibilities. First, maybe the library actually should not include the symbol. This can happen if you use wrong headers during the compilation, and can be debugged by passing the &lt;code&gt;-save-temps&lt;/code&gt; option to gcc and checking the generated &lt;code&gt;.ii&lt;/code&gt; file. Second, the symbol might be almost there—but slightly different—either using different calling convention (on Windows), or &lt;code&gt;wchar_t&lt;/code&gt; mode (also on Windows) or a somewhat different types of parameters, or different namespace. In that case, you'll have to make sure the compiltation options of the application match library's requirements. Finally, it could be that the library actually lacks the symbol due to library bug, and you have to complain to maintainer.  To distinguish those cases, you need to manually examine the list of library symbols.  With gcc, the 'nm' command will do for static libraries, while 'readelf'  can be used on shared libraries (Unix only). I don't know the best way on Windows, suggestions welcome.

&lt;/p&gt;&lt;p&gt;
That's it for the common case. Below, I list some relatively common specific problems. The list does not claim to be complete, so if you know some other cases, drop me a line.

&lt;/p&gt;&lt;p&gt;
&lt;span style="font-weight: bold;"&gt;Static linking&lt;/span&gt;. For static linking, the order of libraries on the command line matters, so if you don't see the linking grabbing the object file with your symbol, you might want to either reorder the libraries or use the &lt;code&gt;--start-group&lt;/code&gt; option. See &lt;a href="http://sourceware.org/binutils/docs/ld/Options.html#Options"&gt;ld documentation&lt;/a&gt; for details and note that the performance cost of the &lt;code&gt;--start-group&lt;/code&gt; option might not be a concern these days.

&lt;/p&gt;&lt;p&gt;
&lt;span style="font-weight: bold;"&gt;References to vtable&lt;/span&gt;. The GNU C++ compiler sometimes reports unresolved reference to 'vtable for SomeClass'. This generally is a pure way to say that the first non-inline method of SomeClass is not defined. See &lt;a href="http://gcc.gnu.org/faq.html#vtables"&gt;GCC FAQ&lt;/a&gt;

&lt;/p&gt;&lt;p&gt;
&lt;span style="font-weight: bold;"&gt;Windows DLLs&lt;/span&gt;. On Windows, if an application wants to use a function in DLL, then both DLL and the application should record this intention, using &lt;code&gt;__declspec(dllexport)&lt;/code&gt; and &lt;code&gt;__declspec(dllimport)&lt;/code&gt; pair. If either party does not do so, linker complains. With mingw, a typical error is &lt;code&gt;undefined reference to `_imp___WHATEVER'&lt;/code&gt;. It means that the library is static, whereas the applications wants to use shared library.

&lt;/p&gt;&lt;p&gt;
&lt;span style="font-weight: bold;"&gt;Windows import libraries&lt;/span&gt;. On Windows, it's not possible to directly link to a DLL. Instead, an import library is created and used—typically by passing &lt;code&gt;/IMPLIB&lt;/code&gt; option to the linker. If the linker does not report any errors, but does not produce import library either, it's a sure sign that you have not exported any function from the DLL, and have to check the logic that adds &lt;code&gt;__declspec(dllexport)&lt;/code&gt;

&lt;/p&gt;&lt;p&gt;
&lt;span style="font-weight: bold;"&gt;64-bit compilation&lt;/span&gt;. When building 64-bit applications, you can get an error that say something about "relocation R_X86_64_32", and suggesting the -fPIC option. The issue here is that 64-bit applications should include only code  compiled with -fPIC, and if you link against any static libraries, those libraries should also be compiled with -fPIC.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7558958-8368718919825441328?l=vladimir_prus.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vladimir_prus.blogspot.com/feeds/8368718919825441328/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=7558958&amp;postID=8368718919825441328' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7558958/posts/default/8368718919825441328'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7558958/posts/default/8368718919825441328'/><link rel='alternate' type='text/html' href='http://vladimir_prus.blogspot.com/2009/06/linking-101.html' title='Linking 101'/><author><name>Vladimir Prus</name><uri>http://www.blogger.com/profile/15916897773452934224</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='16858447227880110707'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7558958.post-320232628064889202</id><published>2009-05-20T20:22:00.004+04:00</published><updated>2009-05-20T20:40:29.383+04:00</updated><title type='text'>KDevelop error display</title><content type='html'>For quite a while I wanted KDevelop to display compilation errors directly inside the editor, as opposed to separate window you have to click in. It works now, as shown below.
&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_DUtp0m1tSPk/ShQuiXO4JzI/AAAAAAAAAFM/gz1hyKS18pI/s1600-h/inline_error.jpg"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 288px;" src="http://4.bp.blogspot.com/_DUtp0m1tSPk/ShQuiXO4JzI/AAAAAAAAAFM/gz1hyKS18pI/s400/inline_error.jpg" alt="" id="BLOGGER_PHOTO_ID_5337942626227726130" border="0" /&gt;&lt;/a&gt;

This was implemented by Ivan Ruchkin, a student at Moscow State University, who will be defending a term paper about various KDevelop-related work tomorrow. The patches will be posted to appropriate mailing lists right after that.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7558958-320232628064889202?l=vladimir_prus.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vladimir_prus.blogspot.com/feeds/320232628064889202/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=7558958&amp;postID=320232628064889202' title='13 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7558958/posts/default/320232628064889202'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7558958/posts/default/320232628064889202'/><link rel='alternate' type='text/html' href='http://vladimir_prus.blogspot.com/2009/05/kdevelop-error-display-and-breadcrumbs.html' title='KDevelop error display'/><author><name>Vladimir Prus</name><uri>http://www.blogger.com/profile/15916897773452934224</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='16858447227880110707'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_DUtp0m1tSPk/ShQuiXO4JzI/AAAAAAAAAFM/gz1hyKS18pI/s72-c/inline_error.jpg' height='72' width='72'/><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>13</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7558958.post-6934388735386112528</id><published>2008-04-19T19:42:00.005+04:00</published><updated>2008-04-19T20:30:16.478+04:00</updated><title type='text'>Variable tooltips</title><content type='html'>&lt;span style="font-family:georgia;"&gt;The most voted-for feature request for KDevelop3 debugger was variable tooltips. I don't think KDevelop3 will ever get them, but KDevelop4 will, as shown below.&lt;/span&gt;

&lt;p&gt;
&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_DUtp0m1tSPk/SAoS-_QWLOI/AAAAAAAAACw/s9H2J8lOXsw/s1600-h/variable_tooltip.png"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer;" src="http://2.bp.blogspot.com/_DUtp0m1tSPk/SAoS-_QWLOI/AAAAAAAAACw/s9H2J8lOXsw/s400/variable_tooltip.png" alt="" id="BLOGGER_PHOTO_ID_5190982393839103202" border="0" /&gt;&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7558958-6934388735386112528?l=vladimir_prus.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vladimir_prus.blogspot.com/feeds/6934388735386112528/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=7558958&amp;postID=6934388735386112528' title='9 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7558958/posts/default/6934388735386112528'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7558958/posts/default/6934388735386112528'/><link rel='alternate' type='text/html' href='http://vladimir_prus.blogspot.com/2008/04/variable-tooltips.html' title='Variable tooltips'/><author><name>Vladimir Prus</name><uri>http://www.blogger.com/profile/15916897773452934224</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='16858447227880110707'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_DUtp0m1tSPk/SAoS-_QWLOI/AAAAAAAAACw/s9H2J8lOXsw/s72-c/variable_tooltip.png' height='72' width='72'/><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>9</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7558958.post-4257384193346886291</id><published>2007-12-21T21:47:00.000+03:00</published><updated>2007-12-24T23:24:51.717+03:00</updated><title type='text'>Debugger stories: pending breakpoints</title><content type='html'>KDevelop 3.5 has a subtle bug. Sometimes, when you step over a function call, you don't stop on the next line. Instead, the application is resumed until it hits a breakpoint, or exits. This bug, in fact, is consequence of how breakpoints in shared libraries are implemented.
&lt;p&gt;
Suppose you've just started a debugger, and try to set a breakpoint on a function in a shared library. The library itself might not be loaded yet, in which case GDB cannot find the address of the symbol to set the low-level breakpoint. To handle this case, starting with version 6.1, GDB supports &lt;span style="font-style: italic;"&gt;pending breakpoints&lt;/span&gt;.  Such breakpoints don't correspond to any address in program, they only keep the specified breakpoint location as string. Whenever a new shared library is loaded, GDB tries to re-parse breakpoint location again, and if that succeeds, creates an ordinary breakpoint.
&lt;p&gt;
Now, this does not work when using the MI interface, for a couple of reasons:
&lt;ul&gt;&lt;li&gt;When a pending breakpoint is resolved, it is deleted, and new one is created. And GDB fails to inform MI frontend about this.
&lt;/li&gt;&lt;li&gt;It's actually not possible to create pending breakpoint using MI at all.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;
Because of these issues (and a bit of historic reasons) KDevelop 3.5 simulates pending breakpoints. GDB is asked to stop whenever a shared library is loaded, and when that happens, KDevelop tries to reinsert breakpoints. This works pretty well, except for the bug I mention in the beginning. Suppose you're stepping over a function call (this uses the "next" command on GDB level). The function opens some shared library, and which point GDB stops and KDevelop tries to reinsert breakpoints. After that KDevelop would like to continue the "next" operation, but it's already aborted by GDB. All we can do is continue the program.
&lt;p&gt;
But it's not longer the case today. As I wrote &lt;a href="http://vladimir_prus.blogspot.com/2007/11/breakpoints-in-constructors.html"&gt; earlier&lt;/a&gt; GDB was recently modified so that a breakpoint can correspond to several addresses, such as of template instantiations. A breakpoint is re-evaluated each time a shared library is loaded or unloaded, and locations are added to breakpoint and removed as appropriate, but it remains the same breakpoint. The nice side effect is that pending breakpoints are now just breakpoints with zero locations, that are reevaluated just like other breakpoints, and don't ever change their number.
&lt;p&gt;
In addition to that, I wrote patches to add pending breakpoint support to MI -- which mainly involved getting rid of two parallel breakpoint-setting code paths -- one for MI and one for CLI. Thanks to review of Joel Brobecker and Daniel Jacobowitz, those patches went in GDB CVS eariler this month. KDevelop 3.5 SVN was modified to automatically detect and use this GDB feature. So, if you're willing to build CVS HEAD of gdb and KDevelop from KDE 3.5 branch, you can finally have breakpoints in shared library just working.
&lt;p&gt;
This was probably my last KDevelop 3.5 commit. KDevelop 4 is ahead.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7558958-4257384193346886291?l=vladimir_prus.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vladimir_prus.blogspot.com/feeds/4257384193346886291/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=7558958&amp;postID=4257384193346886291' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7558958/posts/default/4257384193346886291'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7558958/posts/default/4257384193346886291'/><link rel='alternate' type='text/html' href='http://vladimir_prus.blogspot.com/2007/12/debugger-stories-pending-breakpoints.html' title='Debugger stories: pending breakpoints'/><author><name>Vladimir Prus</name><uri>http://www.blogger.com/profile/15916897773452934224</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='16858447227880110707'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7558958.post-7986361147543872174</id><published>2007-11-26T21:30:00.000+03:00</published><updated>2007-11-26T21:37:22.532+03:00</updated><title type='text'>Breakpoints in constructors</title><content type='html'>Presently, no release of GDB properly handles breakpoints in contructors. This summer, I've worked on fixing that, and while it took longer than expected, it was eventually done, just in time for &lt;a href="http://www.codesourcery.com/gnu_toolchains/sgpp"&gt;Sourcery G++&lt;/a&gt; Fall release. The patches were also submitted for GDB FSF, missed the window for 6.7, but will be present in 6.8 release.

&lt;p&gt;
The underlying problem with breakpoints in constructors was that gcc generates two distinct function bodies for a constructor.  One is a regular one that constructs the entire object, including all bases. Another one constructs everything except for virtual base classes.  As it happens, gcc emits both constructors even for classes that have no virtual bases at all.  GDB was not prepared that a given function name or source line corresponds to several addresses
in program, so it picks one. And usually it picked the wrong one.

&lt;p&gt;
Constructor is the most common case, but is not the only one. If you set a breakpoint in a function template, you can have multiple template instantiations that correspond to a source line. An inline function can be inlined in multiple places, and lead to exactly the same problem.

&lt;p&gt;
The solution, obviously, is to teach GDB that a breakpoint can correspond to several addresses, and then create multiple-location breakpoints when needed. Now, whenever a user creates a breakpoint that resolves to a source line, GDB traverses line tables for all modules, and if it finds another address for the same line, that address is added to breakpoint.  For a template or inline function, you can end up with quite a lot of locations, so you can review list of locations, and disable the unwanted ones.

&lt;p&gt;
The nicest bit of this is interaction with shared libraries. Say, you've set a breakpoint inside function template.  If you load a new shared library, and it contains an instantiation of that function template, a new location will be added to the breakpoint, transparently. If a library is unloaded, the location will become 'pending', until you load the library back.

&lt;p&gt;
The side effect of this work was a serious improvement in the way breakpoints in shared libraries work, but that's a topic for another post.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7558958-7986361147543872174?l=vladimir_prus.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vladimir_prus.blogspot.com/feeds/7986361147543872174/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=7558958&amp;postID=7986361147543872174' title='8 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7558958/posts/default/7986361147543872174'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7558958/posts/default/7986361147543872174'/><link rel='alternate' type='text/html' href='http://vladimir_prus.blogspot.com/2007/11/breakpoints-in-constructors.html' title='Breakpoints in constructors'/><author><name>Vladimir Prus</name><uri>http://www.blogger.com/profile/15916897773452934224</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='16858447227880110707'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>8</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7558958.post-2173552291981966095</id><published>2007-02-01T00:28:00.000+03:00</published><updated>2007-02-01T01:11:52.463+03:00</updated><title type='text'>Debugger Stories: Stack widget</title><content type='html'>In KDevelop 3.4, the stack widget was not changed much. I can remember just two changes­—one that is apparent and one that is subtle.
&lt;p&gt;
The apparent change is that we actually parse gdb output, and show it it a readable way, while in KDevelop 3.3 the stack frame formatting was entirely at mercy of gdb's "backtrace" command.
&lt;p&gt;
&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_DUtp0m1tSPk/RcERPzkh9iI/AAAAAAAAAAc/Lct-6iWkZEU/s1600-h/stack.png"&gt;&lt;img style="margin: 0pt 10px 10px 0pt; float: left; cursor: pointer;" src="http://4.bp.blogspot.com/_DUtp0m1tSPk/RcERPzkh9iI/AAAAAAAAAAc/Lct-6iWkZEU/s400/stack.png" alt="" id="BLOGGER_PHOTO_ID_5026317622361323042" border="0" /&gt;&lt;/a&gt;
&lt;p&gt;
The subtle change is at the bottom of the screenshot—that "(click to get more frame)" thing. When a program stops, KDevelop fetches very few frames from gdb. If you click on that last item, then another chunk of frames will be fetched.
&lt;p&gt;
This behaviour is needed for two reasons. First, if your program is stuck in infinite recursion, and you try to interrupt it from KDevelop, in KDevelop 3.3 you're out of luck. As soon as the program is interrupted, KDevelop asks gdb for the list of all frames. Since your program is in infinite recursion, the number of frames is very large, and gdb is not very speedy stack-walker. So, you get to wait 5 mins for the stack to be shown. With incremental display, in a few clicks you'll see what function went astray.
&lt;p&gt;
The second reason is embarassing. Even without infinite recursion, getting the list of frames from gdb takes a lot of time. Something like half-a-second for getting 30 frames is not unheard of. Ideally, we'd fix gdb, but since we need incremental fetch anyway, fetching sufficiently small number of frames initially greatly improves responsiveness.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7558958-2173552291981966095?l=vladimir_prus.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vladimir_prus.blogspot.com/feeds/2173552291981966095/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=7558958&amp;postID=2173552291981966095' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7558958/posts/default/2173552291981966095'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7558958/posts/default/2173552291981966095'/><link rel='alternate' type='text/html' href='http://vladimir_prus.blogspot.com/2007/02/debugger-stories-stack-widget.html' title='Debugger Stories: Stack widget'/><author><name>Vladimir Prus</name><uri>http://www.blogger.com/profile/15916897773452934224</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='16858447227880110707'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_DUtp0m1tSPk/RcERPzkh9iI/AAAAAAAAAAc/Lct-6iWkZEU/s72-c/stack.png' height='72' width='72'/><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7558958.post-114906807929380237</id><published>2006-05-31T13:12:00.000+04:00</published><updated>2006-06-02T19:45:36.546+04:00</updated><title type='text'>Debugger stories: Watchpoints</title><content type='html'>One of my faviourite debugger changes in KDevelop 3.4 is proper watchpoint handling. Before explaining it, some introduction is due.
&lt;p&gt;
Say you're debugging and see that the 'foo' field of 'pointer_to_some_data' is completely bogus. You are sure that it was valid some time ago, like when its containing object was constructed, so the question is where the corruption happened. That's exactly what watchpoints are for. You set breakpoint at a code where 'foo' is known to be valid, and then ask the debugger to stop whenever the value of 'foo' changes. The debugger in turn writes the address of 'foo' to a special processor register, and processor will call back the operating system, and then debugger, when 'foo' changes.
&lt;p&gt;
Except that GDB does not work this way by default. If you say:
&lt;blockquote&gt;&lt;code&gt;&lt;pre&gt;
watch pointer_to_some_data-&gt;foo
&lt;/pre&gt;&lt;/code&gt;&lt;/blockquote&gt;
there are two interpretations. First is to stop when memory location referred to by &lt;code&gt;pointer_to_some_data-&gt;foo&lt;/code&gt; is modified. Second is to stop when the value of the &lt;span style="font-weight:bold;"&gt;expression&lt;/span&gt; &lt;code&gt;pointer_to_some_data-&gt;foo&lt;/code&gt; changes, which can happen also if &lt;code&gt;pointer_to_some_data&lt;/code&gt; changes. Obviously, when debugging memory corruption, you care about memory address, and &lt;code&gt;pointer_to_some_data&lt;/code&gt; is just a way to specify the memory address. Alas, by default GDB uses the second interpretation, so to set watchpoint on address you should use:
&lt;blockquote&gt;&lt;code&gt;&lt;pre&gt;
print &amp;(pointer_to_some_data-&gt;foo)
watch *$
&lt;/pre&gt;&lt;/code&gt;&lt;/blockquote&gt;
&lt;p&gt;
But the problem is not just that you'll get false hits when &lt;code&gt;pointer_to_some_data&lt;/code&gt; changes. The thing is that if that variable is a local one, or a function parameter, then GDB will immediately &lt;span style="font-weight:bold;"&gt;remove&lt;/span&gt; watchpoint when you exit the containing function. So, for KDevelop user it will be like that: you pick a local variable in a variables widget, you expand it, right-click on some member, select "Toggle watchpoint", and continue. The watchpoint you've just added immediately goes away.
&lt;p&gt;
KDevelop 3.4 solves this problem in a radical way. All watchpoints are address watchpoints. For any expression you enter, address is computed and watchpoint is set on address. Expression without address (rvalue) can't be watched and you'll get an error message if adding watchpoints for rvalue. Additionally, when the application exits, all watchpoints are disabled, because data addresses can well be different on the next run. When user decides to enable a watchpoint, the address of expression is evaluated again, and a new watchpoints is set to that address.
&lt;p&gt;
Hopefully this will make watchpoints more usable for the ordinary programmer.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7558958-114906807929380237?l=vladimir_prus.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vladimir_prus.blogspot.com/feeds/114906807929380237/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=7558958&amp;postID=114906807929380237' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7558958/posts/default/114906807929380237'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7558958/posts/default/114906807929380237'/><link rel='alternate' type='text/html' href='http://vladimir_prus.blogspot.com/2006/05/debugger-stories-watchpoints.html' title='Debugger stories: Watchpoints'/><author><name>Vladimir Prus</name><uri>http://www.blogger.com/profile/15916897773452934224</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='16858447227880110707'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7558958.post-114655429246885688</id><published>2006-05-02T11:17:00.000+04:00</published><updated>2006-05-31T18:42:46.876+04:00</updated><title type='text'>Introducing MI branch</title><content type='html'>For recent months, I was working on an internal reorganization of KDevelop debugger, informally known as "MI branch". Now that it mostly works, it's time to describe the goals and results. 
&lt;p&gt;
The original goal was to use a different interface with GDB, called "MI", that's specifically meant for frontends. In MI mode, gdb output can be easily parsed into DOM-like structure, and examined in a nice C++ way, something like:
&lt;blockquote&gt;&lt;code&gt;&lt;pre&gt;
(*last_stop_result)["value"]["old"].literal()
&lt;/pre&gt;&lt;/code&gt;&lt;/blockquote&gt;
Before, KDevelop was parsing GDB output intended for humans, and could in some cases misinterpret it. Like thinking that application is running, when it's actually stopped. This unreliability was the primary reason for switching to MI.
&lt;p&gt;
But MI is not a silver bullet. Both John Birch (original author of debugger part), and I had reservation about maturity of MI, which proved true eventually. I'll talk about this later, but basically, using MI does not automatically make debugger better, or faster, or anything, contrary to what many think. However, since using different protocol is a big code change already, I've sneaked in a number of architectural and GUI changes, hopefully for better.
&lt;p&gt;
So in the end MI branch had two goals:
&lt;ol&gt;
&lt;li&gt;Fix all glitches. Do you know that in some cases KDevelop 3.3 shows only half of local variables? Or that setting watchpoint in a natural way is rarely what you want? Or that if a program is stuck in infinite recursion, KDevelop will take minutes to show the stack? Each issue is not very significant in itself, but together they make user experience not confortable.
&lt;li&gt;Cleanup internal architecture. Original architecture was a bit too centralized and adding new features required a lot of work. And since many cool features come to mind, it's better be fixed quick.
&lt;/ol&gt;
&lt;p&gt;
The "MI branch" itself is already merged to KDevelop 3.4 branch. In future posts I'll describe all changes the debugger has compared to 3.3 release. Stay tuned.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7558958-114655429246885688?l=vladimir_prus.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vladimir_prus.blogspot.com/feeds/114655429246885688/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=7558958&amp;postID=114655429246885688' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7558958/posts/default/114655429246885688'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7558958/posts/default/114655429246885688'/><link rel='alternate' type='text/html' href='http://vladimir_prus.blogspot.com/2006/05/introducing-mi-branch.html' title='Introducing MI branch'/><author><name>Vladimir Prus</name><uri>http://www.blogger.com/profile/15916897773452934224</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='16858447227880110707'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7558958.post-114613323460364990</id><published>2006-04-27T13:48:00.000+04:00</published><updated>2006-07-07T18:45:37.220+04:00</updated><title type='text'>Non-constant size</title><content type='html'>Quite some time ago, when I was learning &lt;a href="http://www.sgi.com/tech/stl/"&gt;STL&lt;/a&gt;, all information sources stressed the importance of learning complexity guarantees that methods of various containters make. One specific subtle thing is that the &lt;code&gt;std::list&lt;&gt;::size()&lt;/code&gt; method runs in &lt;span style="font-style: italic;"&gt;linear&lt;/span&gt; time, not in constant time. It was explicitly designed that way for a reason,  described in &lt;a href="http://www.sgi.com/tech/stl/FAQ.html"&gt;STL faq&lt;/a&gt;, but what matters to an ordinary programmer is that testing lists for emptyness should be done with the &lt;code&gt;empty&lt;/code&gt; method, not by comparing size to zero, otherwise it's easy to kill performance.
&lt;p&gt;
Today, I run into another case where non-const-time &lt;code&gt;size()&lt;/code&gt; matters. I was testing KDevelop on some testcase, and noticed that getting list of stack frames from gdb takes a lot of time. I've added some profiling code, and found that a one command takes 200ms to execute. Adding profiling code to gdb revealed that gdb itself takes some 70ms. Of course, that's not ideal, but even larger fraction of time was apparently spend in KDevelop, ehm, parsing the response.
&lt;p&gt;
So I've quickly put up a testcase that repeatedly parses a specific response, and ran it under &lt;a href="http://kcachegrind.sourceforge.net/cgi-bin/show.cgi"&gt;callgrind&lt;/a&gt;.  Ten minutes later I've got a profile with &lt;code&gt;strlen&lt;/code&gt; on  top. It turned out that the parsing code was using &lt;code&gt;QCString&lt;/code&gt; and calling it's &lt;code&gt;length&lt;/code&gt; method at least one for each token, and for certain tokens -- once for each character. The &lt;code&gt;length&lt;/code&gt;, in turn, just calls &lt;code&gt;strlen&lt;/code&gt;. Since the input string was 20K in size, most of runtime was spend measuring the size of that string. 
&lt;p&gt;Another unexpected behaviour was found in the &lt;code&gt;QCString::mid&lt;/code&gt; function. Internally, it also calls &lt;code&gt;length&lt;/code&gt;, and &lt;code&gt;mid&lt;/code&gt; was called once for each token.

&lt;p&gt;After uses of non-const-time methods were reduced to minimum, the parsing time my test case decreased 40x. No so bad, I think. The only problem is that time spend in gdb is still to high for a GUI, and that won't be that easy to fix.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7558958-114613323460364990?l=vladimir_prus.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vladimir_prus.blogspot.com/feeds/114613323460364990/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=7558958&amp;postID=114613323460364990' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7558958/posts/default/114613323460364990'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7558958/posts/default/114613323460364990'/><link rel='alternate' type='text/html' href='http://vladimir_prus.blogspot.com/2006/04/non-constant-size.html' title='Non-constant size'/><author><name>Vladimir Prus</name><uri>http://www.blogger.com/profile/15916897773452934224</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='16858447227880110707'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7558958.post-114431922218203677</id><published>2006-04-06T13:57:00.000+04:00</published><updated>2007-09-03T18:27:30.272+04:00</updated><title type='text'>Printf debugging 2006</title><content type='html'>One of the oldest methods of debugging is "printf debugging" -- putting various print statements in the code and then staring at the output. That's useful not only if you can't use a debugger. If the program does not crash, but produces wrong results after long computation, it's hard to figure where exactly the problem lies. In that case, printing intermediate data can be a very efficient method.

&lt;p&gt;
The only problem is that after adding print statements the program must be recompiled, and after debugging print statements must be removed. But it's possible to make gdb into printing machine using so called "breakpoint commands". Each breakpoint can have a list of commands that will be executed when breakpoint is hit. The commands can include printing and "continue". Here's a simplified example of gdb script I've used recently:

&lt;blockquote&gt;&lt;code&gt;&lt;pre&gt;
break main.cpp:1353
commands
   print ('lvk::nm_model::NM'*)this
   printf "Entering 'run', proc %d\n", $-&gt;processor_number
   continue
end
run
&lt;/pre&gt;&lt;/code&gt;&lt;/blockquote&gt;

&lt;p&gt;
After putting this to a file "script", gdb can be run as:
&lt;blockquote&gt;&lt;code&gt;
gdb -batch -x script &lt;span style="font-style:italic;"&gt;name_of_program&lt;/span&gt; &gt; log
&lt;/code&gt;&lt;/blockquote&gt;
producing logs of variable values as certain points of the program.

&lt;p&gt;Starting with version 3.3.0, similar functionality is available in &lt;a href="http://kdevelop.org"&gt;KDevelop&lt;/a&gt;. Just click on the "Tracing" column in breakpoints window, select variables to print and click OK.

&lt;p align="center"&gt;
&lt;img src="http://zigzag.cs.msu.su/~ghost/images/tracing_dialog_shadow.png"&gt;&lt;/img&gt;&lt;/p&gt;

&lt;p&gt;More screenshots &lt;a href="http://kdevelop.org/graphics/screenshots/3.3/tracing2.png"&gt;here&lt;/a&gt; and &lt;a href="http://kdevelop.org/graphics/screenshots/3.3/tracing_output.png"&gt;here&lt;/a&gt;.

This is a beginning, future KDevelop version will allow to specify custom commands for breakpoints.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7558958-114431922218203677?l=vladimir_prus.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vladimir_prus.blogspot.com/feeds/114431922218203677/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=7558958&amp;postID=114431922218203677' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7558958/posts/default/114431922218203677'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7558958/posts/default/114431922218203677'/><link rel='alternate' type='text/html' href='http://vladimir_prus.blogspot.com/2006/04/printf-debugging-2006.html' title='Printf debugging 2006'/><author><name>Vladimir Prus</name><uri>http://www.blogger.com/profile/15916897773452934224</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='16858447227880110707'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7558958.post-114416159039439138</id><published>2006-04-04T18:23:00.000+04:00</published><updated>2006-04-04T18:41:13.196+04:00</updated><title type='text'>Unlucky numbers: 48, 58 and 388</title><content type='html'>One day I've got a remarkable bug report:
&lt;blockquote&gt;We're calling the function from your library, and after 48 successfull calls, it crashes. Can you look into this?&lt;/blockquote&gt;Initially, I was curious how did they count that '48'. It turned out that the application was repatedly doing the same action, calling my code alogn the way, and counting the number of repeatitions, so '48' was the exact number, and the thing conistently crashed after 48 calls.

&lt;p&gt;
The bug report did not include any calling code, so I've asked for the code to be sent, and went home, and while in a bus decided that either it's fixed size buffer in the calling code, or resource leak, like file descriptors leak.

&lt;p&gt;
And sure thing, next morning I looked at the only thing where I used plain &lt;code&gt;FILE*&lt;/code&gt; in order to use Bison-based parser, and there was missing &lt;code&gt;fclose&lt;/code&gt; call. Feeling rather smart, I've sent the fixed version back.

&lt;p&gt;
After 30 minutes new bug report arrived saying that the code fails after 58 calls. And this time, the bug does not reproduces for me. After several tries I found out that for me, the unlucky number is actually 388, so I need to wait a bit to reproduce the bug.

&lt;p&gt;
This was resource leak too, though a subtle one. The library was calling external tool, and modified the &lt;code&gt;PATH&lt;/code&gt; environment to make sure the tool is found. As the result, the length of &lt;code&gt;PATH&lt;/code&gt; variable steadily increased, and finally some OS limit would be reached. After that the value of &lt;code&gt;PATH&lt;/code&gt; becomes completely bogus and the external tool won't be found.

&lt;p&gt;
I'm really glad we have &lt;a href="http://valgrind.org"&gt;Valgrind&lt;/a&gt; so at least memory leaks don't require any magic to debug.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7558958-114416159039439138?l=vladimir_prus.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vladimir_prus.blogspot.com/feeds/114416159039439138/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=7558958&amp;postID=114416159039439138' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7558958/posts/default/114416159039439138'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7558958/posts/default/114416159039439138'/><link rel='alternate' type='text/html' href='http://vladimir_prus.blogspot.com/2006/04/unlucky-numbers-48-58-and-388.html' title='Unlucky numbers: 48, 58 and 388'/><author><name>Vladimir Prus</name><uri>http://www.blogger.com/profile/15916897773452934224</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='16858447227880110707'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7558958.post-113480444320072195</id><published>2005-12-17T10:12:00.000+03:00</published><updated>2005-12-17T10:32:03.236+03:00</updated><title type='text'>Not my bug</title><content type='html'>Often most debugging time is spent of pretty uninteresting bugs.

&lt;p&gt;
For example, most part of last Friday I was staring into debugger backed up by MIPS simulator, trying to figure out why exactly one test of 10 run decided to crash on a MIPS board. Let me first show you the code that was found guilty in the end:
&lt;blockquote&gt;&lt;pre&gt;&lt;code&gt;
static void itoa_really(unsigned long long value, unsigned int base)
{
    static char digits[] = {'0', '1', '2', '3', '4', '5',
                '6', '7', '8', '9', 'a', 'b', 'c', 
                'd', 'e', 'f'};

    if (!value)
        return;
    else {
        long rem;
        itoa_really(value / base, base);
        rem = value % base;
        target_putchar(digits[rem]);
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/blockquote&gt;
This is extremely basic code, but yes, it did broke everything. 

&lt;p&gt;
Initially, the crash happened on a real MIPS board, on a pretty complex test. It was reduced to a 
test printing just two 64-bit values, and crash depended on the printed values. Say, printing &lt;code&gt;0&lt;/code&gt; worked, but printing &lt;code&gt;0x11223344556677&lt;/code&gt; crashed.
It was not possible to debug the problem on the board, and the few floating licences for simulator were in use, so I tried to solve the problem by poking around. At one time I've added an extra line of code before call to "target_putchar", and the line was:

&lt;blockquote&gt;&lt;pre&gt;&lt;code&gt;
rem = 3;
&lt;/code&gt;&lt;/pre&gt;&lt;/blockquote&gt;
Strangely, the crash was gone.

&lt;p&gt;At this point I though that maybe there's some really wrong with 64 on 32 division, and 'rem'
can become wrong, so I replace that "rem = 3" line with:
&lt;blockquote&gt;&lt;pre&gt;&lt;code&gt;
if (rem &lt; 0) { rem = 0; }
if (rem &gt; 15) { rem = 15; }
&lt;/code&gt;&lt;/pre&gt;&lt;/blockquote&gt;
The crash appeared again.

&lt;p&gt;Another try was:
&lt;blockquote&gt;&lt;pre&gt;&lt;code&gt;
if (rem &lt; 3) { rem = 3; }
if (rem &gt; 3) { rem = 3; }
&lt;/code&gt;&lt;/pre&gt;&lt;/blockquote&gt;
and the crash was still there.

&lt;p&gt;Finally, with:
&lt;blockquote&gt;&lt;pre&gt;&lt;code&gt;
if (rem &lt;= 3) { rem = 3; }
if (rem &gt; 3) { rem = 3; }
&lt;/code&gt;&lt;/pre&gt;&lt;/blockquote&gt;
everything worked. Of course, all values was printed as "3333.....333", but it did not crash. Completely buffled, I decided simulator is the only help and eventually we've grabbed the license.

&lt;p&gt;A couple of hours later, I found the one-character fix (literally). The linker config file specified mere 1KB of stack size, and when printing too large values, &lt;code&gt;itoa_really&lt;/code&gt; recursed too deep, overflowing the stack, and overwriting a bunch of program variables. Changing to 4KB of stack set it all right. And what's even more funny, I never ever touched that linker config file, and have no idea what kind of programs are supposed to work on such stack space diet.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7558958-113480444320072195?l=vladimir_prus.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vladimir_prus.blogspot.com/feeds/113480444320072195/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=7558958&amp;postID=113480444320072195' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7558958/posts/default/113480444320072195'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7558958/posts/default/113480444320072195'/><link rel='alternate' type='text/html' href='http://vladimir_prus.blogspot.com/2005/12/not-my-bug.html' title='Not my bug'/><author><name>Vladimir Prus</name><uri>http://www.blogger.com/profile/15916897773452934224</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='16858447227880110707'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7558958.post-113447478632215853</id><published>2005-12-13T14:45:00.000+03:00</published><updated>2005-12-13T14:54:41.056+03:00</updated><title type='text'>Whatever you name them</title><content type='html'>I've just wrote the following code:
&lt;blockquote&gt;&lt;code&gt;&lt;pre&gt;
void on_vector_write(unsigned address, unsigned address)
    {
        if (w_on_vector_write)
            w_on_vector_write(address,
    }
&lt;/pre&gt;&lt;/code&gt;&lt;/blockquote&gt;

It's not complete -- I was just about to pass a second parameter to "w_on_vector_write" when I realized that the two parameters of my function have the same name. But heh, I've just copy-pasted that signature, and the code I've pasted from certainly compiles. Here's how that compiling code looks:
&lt;blockquote&gt;&lt;code&gt;&lt;pre&gt;
boost::function&amp;lt;void (unsigned address,unsigned address)&amp;gt; 
    on_vector_write;
&lt;/pre&gt;&lt;/code&gt;&lt;/blockquote&gt;

Apparently, gcc does not care about parameter names that are used when forming function type. And it does not care about function names in function prototypes either, even in 4.0.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7558958-113447478632215853?l=vladimir_prus.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vladimir_prus.blogspot.com/feeds/113447478632215853/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=7558958&amp;postID=113447478632215853' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7558958/posts/default/113447478632215853'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7558958/posts/default/113447478632215853'/><link rel='alternate' type='text/html' href='http://vladimir_prus.blogspot.com/2005/12/whatever-you-name-them.html' title='Whatever you name them'/><author><name>Vladimir Prus</name><uri>http://www.blogger.com/profile/15916897773452934224</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='16858447227880110707'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7558958.post-113041293697857931</id><published>2005-10-27T15:27:00.000+04:00</published><updated>2005-10-27T15:35:36.993+04:00</updated><title type='text'>Where am I?</title><content type='html'>How can an application or a library find out its own location on Linux?

&lt;p&gt;
Suppose you want an application for Linux to work after being just unpacked to 
some directory. No setting of COOL_APP_HOME environment variable, no creation 
of ~/.coolapprc, nothing. If the application is one self-contained binary, no 
problem, but if there are some resources (icons, or translations, or random 
data files), you need to find them somehow. And since the only action user 
did was unpacking the application, you can only find resources by using a 
path relative to the application's path. But finding application path is not 
straightforward.

&lt;p&gt;
The obvious approach is using &lt;code&gt;argv[0]&lt;/code&gt;. But not quite simple, because &lt;code&gt;argv[0]&lt;/code&gt; can be:
&lt;ol&gt;
&lt;li&gt;An absolute path. You just strip file name from it and get the path to the application.
&lt;li&gt;A relative path. You need to join current directory with that relative path, and strip file name.
&lt;li&gt;Application name without the path. Could happen if application was found via the &lt;tt&gt;PATH&lt;/tt&gt; environment variable. It's necessary to iterate over all &lt;tt&gt;PATH&lt;/tt&gt; elements, trying to find the application there.
&lt;li&gt;Anything. The value of &lt;code&gt;argv[0]&lt;/code&gt; is specified in &lt;code&gt;exec*&lt;/code&gt; call and can be absolutely anything.
&lt;/ol&gt;

&lt;p&gt;
A more reliable method is using the &lt;tt&gt;/proc&lt;/tt&gt; filesystem. The &lt;tt&gt;/proc/&amp;lt;pid&amp;gt;/exe&lt;/tt&gt; is a symbolic link the the application, and &lt;tt&gt;/proc/self&lt;/tt&gt; is the same as &lt;tt&gt;/proc/&amp;lt;pid of current application&amp;gt;&lt;/tt&gt;. So, calling &lt;code&gt;readlink&lt;/code&gt; on &lt;tt&gt;/proc/self/exe&lt;/tt&gt; gives the desired effect.

&lt;p&gt;
But it does not work for shared libraries. Shared library can have its own resources, and might want to find them relatively to &lt;em&gt;library path&lt;/em&gt;. Using &lt;tt&gt;/proc/self/exe&lt;/tt&gt; will give the path to the application, not to the library. The solution here is the &lt;code&gt;dladdr&lt;/code&gt; function, which is Linux extension to the dynamic loader interface. Here's example use:
&lt;blockquote&gt;&lt;code&gt;&lt;pre&gt;
std::string where_am_i()
{
    DL_info info;
    if (dladdr( &amp;where_am_i, &amp;info ) == 0)
    {
        return info.dli_fname; 
    }
    ....
}
&lt;/pre&gt;&lt;/code&gt;&lt;/blockquote&gt;

&lt;p&gt;
The function takes a code address, and returns information about shared library this address belongs to. So, if &lt;code&gt;where_am_i&lt;/code&gt; function is
defined in a shared library, the above code will return the path to that
library. Unfortunately, this works only for dynamic libraries, but not for application. So, for a really reliable solution one has to combine the &lt;code&gt;/proc/self/exe&lt;/code&gt; trick with the &lt;code&gt;dladdr&lt;/code&gt; trick. 

&lt;p&gt;
The only problem is that both tricks are specific to Linux. Why such a basic functionality not in POSIX?

&lt;p&gt;
The last interesting case is when the "resource" your applications uses is a shared library with on-startup linking (not explicit &lt;code&gt;dlopen&lt;/code&gt; linking). The path to the library should be added to dynamic linker search path &lt;em&gt;before&lt;/em&gt; the application is started, so the above tricks won't help. Forgunately, it's not necessary to create helper applications or scripts, you just need an extra options when linking:
&lt;blockquote&gt;&lt;code&gt;&lt;pre&gt;
g++ -o executable -Wl,-R -Wl,'$ORIGIN' executable.o libhelper.so
&lt;/pre&gt;&lt;/code&gt;&lt;/blockquote&gt;

&lt;p&gt;The &lt;tt&gt;-Wl,-R -Wl,'$ORIGIN'&lt;/tt&gt; options adds a new element "$ORIGIN"  to dynamic library search path in the executable, and the dynamic linker will replace &lt;tt&gt;$ORIGIN&lt;/tt&gt; with the path of the executable.

&lt;p&gt;With all those tricks in hand, it's not longer needed to know beforehand the directory where the application will be installed. But I'd still prefer nice builtin support, like Mac OSX bundles.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7558958-113041293697857931?l=vladimir_prus.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vladimir_prus.blogspot.com/feeds/113041293697857931/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=7558958&amp;postID=113041293697857931' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7558958/posts/default/113041293697857931'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7558958/posts/default/113041293697857931'/><link rel='alternate' type='text/html' href='http://vladimir_prus.blogspot.com/2005/10/where-am-i.html' title='Where am I?'/><author><name>Vladimir Prus</name><uri>http://www.blogger.com/profile/15916897773452934224</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='16858447227880110707'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7558958.post-112842449775171310</id><published>2005-10-04T15:14:00.000+04:00</published><updated>2005-10-04T15:52:16.393+04:00</updated><title type='text'>Black or white?</title><content type='html'>What's better: black box testing, or white box testing? In general, neither, 
but sometimes peeking at internals can be a great help.

&lt;p&gt;
One of the project at work now is writing a compiler for simplified C 
(basically, C without structures). And one recent addition was declaring 
several variables in one declaration. The existing test was:
&lt;blockquote&gt;&lt;code&gt;&lt;pre&gt;
int32 v, v2, v3;

int32 main()
{
    v3 = 15;
    v2 = v = 10;
    printf("Result = %d\n", v + v2 + v3);
    return 0;
}
&lt;/pre&gt;&lt;/code&gt;&lt;/blockquote&gt;
and it worked immediately. But there's was a bug, and just as experiment, I've 
asked the author of the test to find the bug. His attempts were:

&lt;ol&gt;
&lt;li&gt;Using duplicate name of variable: &lt;code&gt;int32 v, v2, v3, v;&lt;/code&gt;. That 
produced an error, as expected.
&lt;li&gt;Defining extra local variable "v2". Still no problems.
&lt;li&gt;Initializing some of the variables at definition point, not inside the 
function.
&lt;li&gt;Trying different type of variables.
&lt;li&gt;Moving the code from &lt;code&gt;main&lt;/code&gt; to another function.
&lt;/ol&gt;

&lt;p&gt;At this point, he gave up. The real problem occured at this example:
&lt;blockquote&gt;&lt;code&gt;&lt;pre&gt;
int32 xi = 100, i = xi + 5;

int32 main()
{
    printf("Result = %d\n", i);
}
&lt;/pre&gt;&lt;/code&gt;&lt;/blockquote&gt;
and was caused by creating variables in the reverse order. The language 
grammar is written in the way that makes traversal in the reverse order more 
natural. And so variable &lt;code&gt;i&lt;/code&gt; was initialized before variable 
&lt;code&gt;xi&lt;/code&gt;. Honestly, I can't blaim the tester. It's a kind of bug you 
can think about only if you know that nonterminals can be left-recursive and 
right-recursive. Or see the code.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7558958-112842449775171310?l=vladimir_prus.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vladimir_prus.blogspot.com/feeds/112842449775171310/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=7558958&amp;postID=112842449775171310' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7558958/posts/default/112842449775171310'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7558958/posts/default/112842449775171310'/><link rel='alternate' type='text/html' href='http://vladimir_prus.blogspot.com/2005/10/black-or-white.html' title='Black or white?'/><author><name>Vladimir Prus</name><uri>http://www.blogger.com/profile/15916897773452934224</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='16858447227880110707'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7558958.post-112195666121491074</id><published>2005-07-21T18:18:00.000+04:00</published><updated>2005-07-21T18:40:51.783+04:00</updated><title type='text'>1 + 2</title><content type='html'>I've just got the following:
&lt;blockquote&gt;&lt;code&gt;&lt;pre&gt;
(gdb) target remote :1234
Remote debugging using :1234
main () at main.cpp:5
5       {
(gdb) p add(1, 2)
$1 = 3
&lt;/pre&gt;&lt;/code&gt;&lt;/blockquote&gt;

As you see, using the gdb debugger I've evaluated expression &lt;code&gt;add(1,2)&lt;/code&gt;, where function &lt;code&gt;add&lt;/code&gt; presumably adds two numbers, and got the right result. What's interesting in this?

There are two interesting things:
&lt;ol&gt;
&lt;li&gt; It's gdb port to a new target.
&lt;li&gt; The remote side is not a hardware board -- it's our simulator, which was taught to talk using gdb's remote protocol
&lt;/ol&gt;

However simple the example seems, it involves several steps:
&lt;ol&gt;
&lt;li&gt; First of all, gdb must load the symbol tables. Othewise, it won't know that &lt;code&gt;add&lt;/code&gt; is a function. That did not work out of box, for example gdb assumed that program can't start at address 0.
&lt;li&gt; After connecting to the target, gdb asks about the value of program counter. This requires support for appropriate protocol command. Also, our simulator took shortcuts and did not even maintain the program counter!
&lt;li&gt; To call a function, the arguments are pushed to a temporary memory area. This requires
code that knows calling convention for the target, and memory writing support.
&lt;li&gt; After that, a fake return address is placed to a stack (typically, entry point of the program), and a breakpoint is set to that return address. So this requires working breakpoints in the simulator.
&lt;li&gt; Then, debugger changes program counter to the address of &lt;code&gt;add&lt;/code&gt; and resumes program execution. 
&lt;li&gt; After hitting breakpoint, the return value of the function is extracted, which again requires knowing calling convention.
&lt;/ol&gt;

So, it's not just a poor man's calculator, it's also a test for a good deal of debugger functionality. There's also stack frame handling (up/down/where/finish), and printing of complex data types (structures/arrays), but believe me, that works as well.

It looks like my 3-month gdb and simulator hacking comes to an end, and I'll spend more time on more interesting things like improving KDevelop's debugger frontend.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7558958-112195666121491074?l=vladimir_prus.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vladimir_prus.blogspot.com/feeds/112195666121491074/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=7558958&amp;postID=112195666121491074' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7558958/posts/default/112195666121491074'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7558958/posts/default/112195666121491074'/><link rel='alternate' type='text/html' href='http://vladimir_prus.blogspot.com/2005/07/1-2.html' title='1 + 2'/><author><name>Vladimir Prus</name><uri>http://www.blogger.com/profile/15916897773452934224</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='16858447227880110707'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7558958.post-112064120291181992</id><published>2005-07-06T13:13:00.000+04:00</published><updated>2005-07-06T13:30:11.840+04:00</updated><title type='text'>Spurious wakeups</title><content type='html'>&lt;p class="mobile-post"&gt;
One of the two basic synchronisation primitives in multithreaded programming is 
called "condition variables". Here's 
a small example:&lt;/p&gt;&lt;p class="mobile-post"&gt;
&lt;blockquote&gt;&lt;code&gt;&lt;pre&gt;
bool something_happened;
boost:::mutex m;
boost:::condition c;
void thread1()
{
     boost::mutex::scoped lock(m);
     while(!something_happened)
     {
        c.wait(m);
     }
}
void thread2()
{
      // do lots of work
      boost::mutex::scoped lock(m);
      something_happened = true;
      c.notify()
}
&lt;/pre&gt;&lt;/code&gt;&lt;/blockquote&gt;&lt;/p&gt;&lt;p class="mobile-post"&gt;Here, the call to "c.wait()" unlocks the mutex (allowing the other thread to 
eventually lock it), and suspends the calling thread. When another thread 
calls 'notify', the first thread wakes up, locks the mutex again (implicitly, 
inside 'wait'), sees that variable is set to 'true' and goes on.&lt;/p&gt;&lt;p class="mobile-post"&gt;But why do we need the while loop, can't we write:
&lt;blockquote&gt;&lt;code&gt;&lt;pre&gt;
if (!something_happened)
    c.wait(m);
&lt;/pre&gt;&lt;/code&gt;&lt;/blockquote&gt;&lt;/p&gt;&lt;p class="mobile-post"&gt;We can't. And the killer reason is that 'wait' can return without any 'notify' 
call. That's called &lt;em&gt;spurious wakeup&lt;/em&gt; and is explicitly allowed by 
POSIX. Essentially, return from 'wait' only indicates that the shared data 
&lt;em&gt;might&lt;/em&gt; have changed, so that data must be evaluated again. &lt;/p&gt;&lt;p class="mobile-post"&gt;Okay, so why this is not fixed yet? The first reason is that nobody wants to 
fix it. Wrapping call to 'wait' in a loop is very desired for several other 
reasons. But those reasons require explanation, while spurious wakeup is a 
hammer that can be applied to any first year student without fail.&lt;/p&gt;&lt;p class="mobile-post"&gt;The second reason is that fixing this is supposed to be hard. Most sources 
I've seen say that fixing that would require very large overhead on certain 
architectures. Strangely, no details were ever given, which made me wonder if 
avoiding spurious wakeups is simple, but all the threading experts secretly 
decided to tell everybody it's hard.&lt;/p&gt;&lt;p class="mobile-post"&gt;After asking on comp.programming.thread, I at least know the reason for Linux (thanks to &lt;a 
href="http://groups-beta.google.com/group/comp.programming.threads/msg/aca08ebdab2cbf52"&gt;Ben 
Hutchings&lt;/a&gt;). Internally, wait is implemented as 
a call to the 'futex' system call. Each blocking system call on Linux returns 
abruptly when the process receives a signal -- because calling signal handler 
from kernel call is tricky. What if the signal handler calls 
some other system function? And a new signal arrives? It's easy to run out of 
kernel stack for a process. Exactly because each system call can be 
interrupted, when glibc calls any blocking function, like 
'read', it does it in a loop, and if  'read' returns EINTR, calls 'read' 
again.&lt;/p&gt;&lt;p class="mobile-post"&gt;Can the same trick be used to conditions? No, because the moment we return 
from 'futex' call, another thread can send us notification. And since we're 
not waiting inside 'futex', we'll miss the notification. So, we need to 
return to the caller, and have it reevaluate the predicate. If another thread 
indeed set it to true, we'll break out of the loop.&lt;/p&gt;&lt;p class="mobile-post"&gt;So much for spurious wakeups on Linux. But I'm still very interested to know 
what the original reasons were.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7558958-112064120291181992?l=vladimir_prus.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vladimir_prus.blogspot.com/feeds/112064120291181992/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=7558958&amp;postID=112064120291181992' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7558958/posts/default/112064120291181992'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7558958/posts/default/112064120291181992'/><link rel='alternate' type='text/html' href='http://vladimir_prus.blogspot.com/2005/07/spurious-wakeups.html' title='Spurious wakeups'/><author><name>Vladimir Prus</name><uri>http://www.blogger.com/profile/15916897773452934224</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='16858447227880110707'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7558958.post-111358667550060016</id><published>2005-04-15T21:37:00.000+04:00</published><updated>2005-04-18T16:05:39.150+04:00</updated><title type='text'>Environment variables gotcha</title><content type='html'>&lt;p class="mobile-post"&gt;
The Debian Policy requires that applications do not &lt;a 
href="http://www.debian.org/doc/debian-policy/ch-opersys.html#s9.9"&gt;depend on 
environment variables&lt;/a&gt; for proper operation. Why? They say that 
&lt;tt&gt;/etc/profile&lt;/tt&gt; is not supported by all shells (hmm... is there 
anything but bash?), and that the file should not be modified at all, but 
there's also another simple reason.&lt;/p&gt;&lt;p class="mobile-post"&gt;If you run:
&lt;blockquote&gt;&lt;code&gt;&lt;pre&gt;
$ su root
# apt-get install whatever
$ whatever
&lt;/pre&gt;&lt;/code&gt;&lt;/blockquote&gt;
then any environment variable setting added to /etc/profile won't affect the 
current session, only future ones. And there's no way around this. So if 
&lt;code&gt;whatever&lt;/code&gt; needs some environment variables, it will just break.&lt;/p&gt;&lt;p class="mobile-post"&gt;In my case, "whatever" used environment variables to find its icons, which 
produced marvellous results:&lt;/p&gt;

&lt;p align="center"&gt;
&lt;img align="center" 
src="http://zigzag.cs.msu.su/~ghost/images/toolbar.png"&gt;&lt;/img&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7558958-111358667550060016?l=vladimir_prus.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vladimir_prus.blogspot.com/feeds/111358667550060016/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=7558958&amp;postID=111358667550060016' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7558958/posts/default/111358667550060016'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7558958/posts/default/111358667550060016'/><link rel='alternate' type='text/html' href='http://vladimir_prus.blogspot.com/2005/04/environment-variables-gotcha.html' title='Environment variables gotcha'/><author><name>Vladimir Prus</name><uri>http://www.blogger.com/profile/15916897773452934224</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='16858447227880110707'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7558958.post-111295664595055162</id><published>2005-04-08T14:37:00.000+04:00</published><updated>2005-04-08T14:43:51.053+04:00</updated><title type='text'>Just multiplication</title><content type='html'>&lt;p class="mobile-post"&gt;
How easy is it to multiply two numbers?&lt;/p&gt;&lt;p class="mobile-post"&gt;Recently, I've discovered that our fast emulator handles the multiplication in 
a completely wrong way. Well, multiplication is a very common operation, and 
even trivial testing should have found the bug. But there's one detail -- the 
processor does not have a multiplication instruction at all. &lt;/p&gt;&lt;p class="mobile-post"&gt;Instead, there are two instructions -- "first multiplication step" (MLF) and 
"next multiplication step" (MLS). To multiply two 32-bit numbers one has to 
execute MLF, and then to execute MLS 15 times. The documentation says this, C 
compiler generates this sequence, and everywhere I looked it was implemented 
this way. So, I decided to ignore MLS, and implement MLF as a regular 
multiplication.&lt;/p&gt;&lt;p class="mobile-post"&gt;And there were just one place where this approach failed, because there were 
11 MLS instructions, not 15.  To explain why this "incorrect" multiplication 
is need, I need to first explain the underlying algorithm.&lt;/p&gt;&lt;p class="mobile-post"&gt;Say you need to multiply 101 (binary) by 11100. Recalling school math that 
would be:
&lt;pre&gt;
         101   
   *   11100
--------------
         101    * 0
+       1010    * 0
+      10100    * 1
+     101000    * 1
+    1010000    * 1&lt;/p&gt;&lt;p class="mobile-post"&gt;=   10001100
&lt;/pre&gt;&lt;/p&gt;&lt;p class="mobile-post"&gt;However, 11100 can be represented as 100000 - 100. So:&lt;/p&gt;&lt;p class="mobile-post"&gt;&lt;pre&gt;
         101   
   *   11100
------------
-      10100    * 1
+   10100000    * 1
&lt;/pre&gt;&lt;/p&gt;&lt;p class="mobile-post"&gt;Both approaches can be implemented as a loop over bits of the second operand. 
In the first approach you look at the lowest bit of the second operand, and 
if it's non-zero, add the first operand to the result. The first operand is 
then shifted left by one bit.&lt;/p&gt;&lt;p class="mobile-post"&gt;In the second approach when the lowest bit of the second operand is non-zero 
and the previous bit was zero, we substract the first operand. When we see 
1-&gt;0 transition, we add the first operand. And the first operand is shifted 
after each bit, too.&lt;/p&gt;&lt;p class="mobile-post"&gt;This is called Booth algorithm, and it's primary purpose was to reduce the 
number of additions and substractions, because on older computers those 
operations took more time then shifts. In the example above, we save one add 
operation. The second advantage, more relevant today, is that Booth algorithm 
works fine with negative numbers. This is not obvious, but it does. See &lt;a 
href="http://turing.une.edu.au/~comp283/cdrom/Content/COD3e/InMoreDepth/IMD3-Booths-Algorithm.pdf"&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;p class="mobile-post"&gt;The MLF and MLS instruction implement the modified Booth algorithm, that handles 
two bits at one step -- so we need 16 steps for 32 bit numbers. In the specific processor, each step 
shifts the second operator right by 2 bits, and new bits of the result appear 
from the left. And now the key trick -- suppose that input operands are 
unsigned and limited to 8 bits. Then the result will contain at most 16 bits. 
After 8 steps will will compute those 16 bits, but they'll be in the upper 
half of the register. Next 8 steps will just slowly shift them to the right 
position. So if you don't mind getting shifted result, you can get it in half 
the time. As an example, if you want to compare a*b with c*d, the extra shift does not matter.&lt;/p&gt;&lt;p class="mobile-post"&gt;Cool, isn't it? But don't ask how long it took me to find the single place where this 
trick was used!&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7558958-111295664595055162?l=vladimir_prus.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vladimir_prus.blogspot.com/feeds/111295664595055162/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=7558958&amp;postID=111295664595055162' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7558958/posts/default/111295664595055162'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7558958/posts/default/111295664595055162'/><link rel='alternate' type='text/html' href='http://vladimir_prus.blogspot.com/2005/04/just-multiplication.html' title='Just multiplication'/><author><name>Vladimir Prus</name><uri>http://www.blogger.com/profile/15916897773452934224</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='16858447227880110707'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7558958.post-111165974743402989</id><published>2005-03-24T13:22:00.000+03:00</published><updated>2005-03-24T13:47:26.013+03:00</updated><title type='text'>Duplicate function bodies</title><content type='html'>&lt;p class="mobile-post"&gt;
A couple of days ago I've learned that the Microsoft linker can &lt;a 
href="http://blogs.msdn.com/oldnewthing/archive/2005/03/22/400373.aspx"&gt;merge 
functions with binary identical bodies&lt;/a&gt;. Looks rather good idea, 
especially with templates, and it seems that gcc does not implement this. Why  
don't estimate possible size saving?&lt;/p&gt;&lt;p class="mobile-post"&gt;Basically, we just need to get all function bodies from an object file or 
shared library and check for duplicates. The only thing to watch out are 
relocations. Consider this code:
&lt;blockquote&gt;&lt;code&gt;&lt;pre&gt;
extern int a, b;
int foo() { return a; }
int bar() { return b; }
&lt;/pre&gt;&lt;/code&gt;&lt;/blockquote&gt;
The binary code for both functions will be identical:
&lt;blockquote&gt;&lt;pre&gt;
   _Z3foov:
55                    pushl   %ebp
89E5                  movl    %esp, %ebp
A1000000              movl    a, %eax
00
5D                    popl    %ebp
C3                    ret
&lt;/pre&gt;&lt;/blockquote&gt;
But later, either during static link or during application startup, those 
eight zeros will be replaced with addresses of variables &lt;code&gt;a&lt;/code&gt; and 
&lt;code&gt;b&lt;/code&gt;, which are obviously different.&lt;/p&gt;&lt;p class="mobile-post"&gt;Luckily, this is easy to fix. When compiling with -fPIC, the compiler will 
produce code that's not modified during startup. This means that two 
identical function bodies will remain identical. Since all shared libraries 
are built with -fPIC, we can safely use shared libraries for testing.&lt;/p&gt;&lt;p class="mobile-post"&gt;However, running the &lt;a 
href="http://zigzag.cs.msu.su/~ghost/blog_data/function_duplicates.hpp"&gt;test 
program&lt;/a&gt; on a couple of template-extensive Boost libraries produce  rather 
discouraging results.&lt;/p&gt;&lt;p class="mobile-post"&gt;&lt;a href="http://boost.org/libs/python"&gt;Boost.Python&lt;/a&gt;:
&lt;blockquote&gt;&lt;pre&gt;
461 functions found
2073 unique function bodies
205589 bytes of function bodies
Function merging would have saved 3898 bytes
&lt;/pre&gt;&lt;/blockquote&gt;&lt;/p&gt;&lt;p class="mobile-post"&gt;&lt;a href="http://boost.org/libs/regex"&gt;Boost.Regex&lt;/a&gt;:
&lt;blockquote&gt;&lt;pre&gt;
3313 functions found
2782 unique function bodies
484635 bytes of function bodies
Function merging would have saved 7808 bytes
&lt;/pre&gt;&lt;/blockquote&gt;&lt;/p&gt;&lt;p class="mobile-post"&gt;Most of the functions with duplicate bodies were rather small in size  -- 5-10 
bytes -- basically empty. Probably, there's some other variable I did not 
consider. Or maybe gcc's register allocator is non-deterministic. What is the 
actual problem is a topic for some future post.&lt;/p&gt;&lt;p class="mobile-post"&gt;As an aside, I've used the BFD library to read the files in ELF format, and got rather 
mixed feelings. On one hand, I've did what I wanted. On the other hand, the 
library is horrible. The documentation structure is the worst I've ever seen, 
and it's very hard to find anything. There's no complete compilable example 
to get started. The interface is terrible C -- with the need to manually 
allocate memory all over. And finally, I was hit by two weird things.&lt;/p&gt;&lt;p class="mobile-post"&gt;First, is that the following reasonably-looking code does not work:
&lt;blockquote&gt;&lt;code&gt;&lt;pre&gt;
bfd* object = bfd_openr("object.o", "i686-pc-linux-gnu");
assert(object);
asection* s = bfd_get_section_by_name(object, ".text");
&lt;/pre&gt;&lt;/code&gt;&lt;/blockquote&gt;
The second call just segfaults. Changing the code to
&lt;blockquote&gt;&lt;code&gt;&lt;pre&gt;
bfd* object = bfd_openr("object.o", "i686-pc-linux-gnu");
assert(object);
assert(bfd_check_format (object, bfd_object));
asection* s = bfd_get_section_by_name(object, ".text");
&lt;/pre&gt;&lt;/code&gt;&lt;/blockquote&gt;
makes it work fine. That's just voodoo.&lt;/p&gt;&lt;p class="mobile-post"&gt;Another problem, is that is not possible to &lt;b&gt;get the size of a symbol&lt;/b&gt;. 
Really, this basic information can't be obtained via BFD's public interface.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7558958-111165974743402989?l=vladimir_prus.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vladimir_prus.blogspot.com/feeds/111165974743402989/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=7558958&amp;postID=111165974743402989' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7558958/posts/default/111165974743402989'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7558958/posts/default/111165974743402989'/><link rel='alternate' type='text/html' href='http://vladimir_prus.blogspot.com/2005/03/duplicate-function-bodies.html' title='Duplicate function bodies'/><author><name>Vladimir Prus</name><uri>http://www.blogger.com/profile/15916897773452934224</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='16858447227880110707'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7558958.post-111142292045124279</id><published>2005-03-21T19:35:00.000+03:00</published><updated>2005-03-21T19:39:25.196+03:00</updated><title type='text'>Why constructors crash</title><content type='html'>&lt;p class="mobile-post"&gt;
Of course, C++ constructors can crash for absolutely any reason. The 
interesting reason is member initialization. Consider this code:
&lt;blockquote&gt;&lt;code&gt;
C::C(const std::string&amp; a) : a(a) {}
&lt;/code&gt;&lt;/blockquote&gt;
which is perfectly OK. The C++ standard allows to use parameter 'a' when 
initializing member 'a'. But consider the following example:
&lt;blockquote&gt;&lt;code&gt;
C::C(const std::string&amp; acuracy) : accuracy(accuracy) {}
&lt;/code&gt;&lt;/blockquote&gt;
A small typo causes initialization of the member with itself, which can likely 
crash. This is one reason why adding "m_" prefix to member names is a good 
idea.&lt;/p&gt;&lt;p class="mobile-post"&gt;Just now, I've seen a more complicated example. A colleague first shown a code 
like in the first example and asked if it's OK. I immediately replied that 
it's OK unless there's a typo. But the real crash was a bit more contrived:
&lt;blockquote&gt;&lt;code&gt;&lt;pre&gt;
class Entry {
public:
     Entry(const std::string&amp; EntryId) : EntryId(EntryId) {}
     std::string EntryId;
};
class LogEntry : public Entry
{
public:
    LogEntry(const std::string&amp; Id): Entry(EntryId) {}    
};
&lt;/pre&gt;&lt;/code&gt;&lt;/blockquote&gt;
Here, if the parameter of the second constructor was named "EntryId", 
everything would work. As it stands, the base constructor is passed the base 
member as initializer and again the member is initialized with itself.&lt;/p&gt;&lt;p class="mobile-post"&gt;Moral of the story
&lt;ol&gt;
&lt;li&gt;Use the "m_" prefix
&lt;li&gt;Make all data members private
&lt;/ol&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7558958-111142292045124279?l=vladimir_prus.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vladimir_prus.blogspot.com/feeds/111142292045124279/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=7558958&amp;postID=111142292045124279' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7558958/posts/default/111142292045124279'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7558958/posts/default/111142292045124279'/><link rel='alternate' type='text/html' href='http://vladimir_prus.blogspot.com/2005/03/why-constructors-crash.html' title='Why constructors crash'/><author><name>Vladimir Prus</name><uri>http://www.blogger.com/profile/15916897773452934224</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='16858447227880110707'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7558958.post-111113091986224935</id><published>2005-03-18T10:28:00.000+03:00</published><updated>2006-02-03T11:05:48.030+03:00</updated><title type='text'>Outputting T</title><content type='html'>&lt;p class="mobile-post"&gt;
I'm positively shocked. In C++, there's no easy way to write a template 
function with one parameter that will output that parameter to a stream, with 
full precision.&lt;/p&gt;&lt;p class="mobile-post"&gt;If you write:
&lt;blockquote&gt;&lt;code&gt;
std::cout &lt;&lt; t;
&lt;/code&gt;&lt;/blockquote&gt;
then you'll be using the default stream precision, which is 6. And 't' can be 
&lt;code&gt;double&lt;/code&gt; with 10 digits after the dot. &lt;/p&gt;&lt;p class="mobile-post"&gt;If you write:
&lt;blockquote&gt;&lt;code&gt;
std::cout &lt;&lt; setprecision(10000) &lt;&lt; t;
&lt;/code&gt;&lt;/blockquote&gt;
you'll suddenly get too many digits. For outputting "1.1", you'll get:
&lt;blockquote&gt;&lt;verbatim&gt;
1.100000000000000088817841970012523233890533447265625
&lt;/verbatim&gt;&lt;/blockquote&gt;
even though real precision of double is &lt;a 
href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclang/html/_clang_Type_float.asp"&gt;15-16&lt;/a&gt; 
digits.&lt;/p&gt;&lt;p class="mobile-post"&gt;If you write:
&lt;blockquote&gt;&lt;code&gt;
std::cout &lt;&lt; setprecision(fancy_formula_involving(numeric_limits&amp;lt;T&amp;gt;::digits) 
&lt;&lt; t;
&lt;/code&gt;&lt;/blockquote&gt;
the code won't compile if T is "char[N]". And if numeric_limits is not 
specialized for a given T, results can also be strange.&lt;/p&gt;&lt;p class="mobile-post"&gt;Oops!&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7558958-111113091986224935?l=vladimir_prus.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vladimir_prus.blogspot.com/feeds/111113091986224935/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=7558958&amp;postID=111113091986224935' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7558958/posts/default/111113091986224935'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7558958/posts/default/111113091986224935'/><link rel='alternate' type='text/html' href='http://vladimir_prus.blogspot.com/2005/03/outputting-t.html' title='Outputting T'/><author><name>Vladimir Prus</name><uri>http://www.blogger.com/profile/15916897773452934224</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='16858447227880110707'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7558958.post-111097771546722754</id><published>2005-03-16T15:55:00.000+03:00</published><updated>2005-03-16T15:55:15.470+03:00</updated><title type='text'>Generalizing from examples</title><content type='html'>&lt;p class="mobile-post"&gt;
Over time I wrote quite a lot of C++ code that generates other C++ code. At 
work I generate C++ from assembler, and I also worked on a parser generator 
called &lt;a href="http://www.cs.queensu.ca/home/okhotin/whale"&gt;Whale&lt;/a&gt;. Code 
generation is a powerful approach, as shown by &lt;a 
href="http://www.kdedevelopers.org/node/view/640"&gt;some other examples&lt;/a&gt;. 
And unlike template metaprogramming it does not stress today's compilers too 
much (for example, compilation speed is a recurring topic on &lt;a 
href="http://spirit.sourceforge.net"&gt;Boost.Spirit&lt;/a&gt; mailing list. &lt;/p&gt;&lt;p class="mobile-post"&gt;But it's damn hard to write the code generator. When writing C++ code as a C++ 
literal, the look of the code is so strange that typos can hide anywhere. 
Then you build the code generator, use it to produce test code, and it fails 
to compile. And you're back to the beginning, hunting for missing semicolons.&lt;/p&gt;&lt;p class="mobile-post"&gt;The simple solution I've recently found is that new functionality should never 
be directly implemented in a code generator. First, you need to take the 
generated code and modify it, despite comments warning you that all changes 
will be lost. Once you've modified the generated code and it works, it's very 
easy to bring modifications back to code generator -- it can be even done by 
regex in emacs. And the generated code will work.&lt;/p&gt;&lt;p class="mobile-post"&gt;This brings us to the topic of the article. It's hardly a good idea to use 
very generic solution without trying specific examples. And an example is 
better that a generic advice, so here's another example: don't write a C++ 
template function from scratch. First write non-template function and debug 
it, and only then add template declaration and try with different template 
parameters. &lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7558958-111097771546722754?l=vladimir_prus.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vladimir_prus.blogspot.com/feeds/111097771546722754/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=7558958&amp;postID=111097771546722754' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7558958/posts/default/111097771546722754'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7558958/posts/default/111097771546722754'/><link rel='alternate' type='text/html' href='http://vladimir_prus.blogspot.com/2005/03/generalizing-from-examples.html' title='Generalizing from examples'/><author><name>Vladimir Prus</name><uri>http://www.blogger.com/profile/15916897773452934224</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='16858447227880110707'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7558958.post-111058780737907531</id><published>2005-03-12T03:36:00.000+03:00</published><updated>2005-03-12T03:36:47.380+03:00</updated><title type='text'>One thing at a time</title><content type='html'>&lt;p class="mobile-post"&gt;
Changing too many things at the same time can easily get one into a problem.&lt;/p&gt;&lt;p class="mobile-post"&gt;My project at work is essentially a very fast emulator, which takes a program 
for a certain processor in source form (C++ and assembler) and creates a 
program which can be executed on a regular PC. Until recently, processing of 
assembler and C++ sources was radically different. That lead to a number of 
problems and we decided to change the approach and compile C++ into assembler 
using the provided SDK. Then we'll get only assembler sources to process.&lt;/p&gt;&lt;p class="mobile-post"&gt;Roughly at the same time, we got new SDK, and I've decided to try it. The 
great news were that the assembler can be run under Wine without problems. 
Excellent, so I've switched to this new SDK immediately.&lt;/p&gt;&lt;p class="mobile-post"&gt;But then, I've also inadvertently updated my Boost copy.&lt;/p&gt;&lt;p class="mobile-post"&gt;As the net result, I have a number of failing tests with the new version of 
the program, new version of SDK (which generates somewhat different code) and 
new version of Boost, and no clue where to start debugging.&lt;/p&gt;&lt;p class="mobile-post"&gt;Of course, at least the problem with Boost could have being prevented by using 
Subversion and reporting revision number during nightly build, but one thing 
at a time -- proper handling of 3rd party dependencies is a topic for another 
post.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7558958-111058780737907531?l=vladimir_prus.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vladimir_prus.blogspot.com/feeds/111058780737907531/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=7558958&amp;postID=111058780737907531' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7558958/posts/default/111058780737907531'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7558958/posts/default/111058780737907531'/><link rel='alternate' type='text/html' href='http://vladimir_prus.blogspot.com/2005/03/one-thing-at-time.html' title='One thing at a time'/><author><name>Vladimir Prus</name><uri>http://www.blogger.com/profile/15916897773452934224</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='16858447227880110707'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry></feed>